#include "../inc/hart_slave_frame.h"
static BOOL has_mock = FALSE; // 测试使用该变量判断是否重入
// 模拟用户自定义动态变量
static void mock_user_dynamics(void)
{
    static float32 input_current = 4.1;                // 输入电流
    static uint16_t valve_feedback = 100;              // 阀位反馈
    static uint16_t atmospheric_pressure_source = 100; // 气压源压力
    static uint16_t pressure_at_port_a = 100;          // A口压力
    static uint16_t pressure_at_port_b = 100;          // B口压力
    static uint16_t amplifier_circuit = 100;           // 放大器回路
    static float32 built_in_temperature = 50.3;        // 内置温度
    static uint16_t ip_output_detection = 100;         // IP输出检测
    static float32 valve_percentage = 20.5;            // 阀位百分比

    static float32 target_row = 10.1;  // 目标行程
    static float32 current_row = 9.1;  // 当前行程
    static float32 friction = 10.5;    // 摩擦力
    static float32 spring_force = 5.5; // 弹簧力

    analog_quantity_t *analog_quantity = &hart_device_attribute.dynamics.dynamics_user.analog_quantity;
    analog_quantity->input_current = &input_current;
    analog_quantity->valve_feedback = &valve_feedback;
    analog_quantity->atmospheric_pressure_source = &atmospheric_pressure_source;
    analog_quantity->pressure_at_port_a = &pressure_at_port_a;
    analog_quantity->pressure_at_port_b = &pressure_at_port_b;
    analog_quantity->amplifier_circuit = &amplifier_circuit;
    analog_quantity->built_in_temperature = &built_in_temperature;
    analog_quantity->ip_output_detection = &ip_output_detection;
    analog_quantity->valve_percentage = &valve_percentage;

    digital_quantity_t *digital_quantity = &hart_device_attribute.dynamics.dynamics_user.digital_quantity;
    digital_quantity->input_current = &input_current;
    digital_quantity->valve_feedback = &valve_feedback;
    digital_quantity->atmospheric_pressure_source = &atmospheric_pressure_source;
    digital_quantity->pressure_at_port_a = &pressure_at_port_a;
    digital_quantity->pressure_at_port_b = &pressure_at_port_b;
    digital_quantity->amplifier_circuit = &amplifier_circuit;
    digital_quantity->built_in_temperature = &built_in_temperature;
    digital_quantity->ip_output_detection = &ip_output_detection;
    digital_quantity->target_row = &target_row;
    digital_quantity->current_row = &current_row;
    digital_quantity->friction = &friction;
    digital_quantity->spring_force = &spring_force;
}

// 模拟用户自定义属性
static void mock_user_attribute(void)
{
}

// 模拟设备属性
static void mock_device_attribute(void)
{
    static uint8_t poll_address = 0x00;
    static uint8_t long_address[5] = {0x00, 0x00, 0x00, 0x00, 0x00};
    static float32 primary_variable_loop_current = 4.0;
    static float32 primary_variable_range_percentage = 10.0;
    static float32 battery_day = 10.0; // 电池寿命
    static float32 default_value = 12.34;
    static uint8_t transducer_serial_number[3] = {0x01, 0xE2, 0x40};
    static uint8_t final_assembly_number[3] = {0xA0, 0x00, 0x01};
    static uint8_t last_code = DEVICE_VARIABLE_CODE_246;
    static uint16_t configuration_update_count = 2;
    uint8_t tag[HART_PACKED8_LEN] = "LAPOOL";
    uint8_t long_tag[HART_PACKED32_LEN] = "LAPOOL-LONG";
    uint8_t descriptor[HART_PACKED16_LEN] = "LAPOOL-01";
    uint8_t message[HART_PACKED32_LEN] = "DEVICE RESTART!";
    analog_channel_t *analog_channel = NULL;

    // flash变量
    osel_memset((uint8_t *)&hart_device_attribute.flash_variable, 0, sizeof(hart_storage_variable_t));
    hart_device_attribute.flash_variable.write_protect_code = WRITE_PROTECT_CODE_NOT_WRITE_PROTECT;
    hart_device_attribute.flash_variable.s2m_preambles = HART_PREAMBLE_DEFAULT_LEN;
    osel_memcpy(hart_device_attribute.flash_variable.tag, tag, ARRAY_LEN(tag));
    osel_memcpy(hart_device_attribute.flash_variable.long_tag, long_tag, ARRAY_LEN(long_tag));
    osel_memcpy(hart_device_attribute.flash_variable.descriptor, descriptor, ARRAY_LEN(descriptor));
    hart_device_attribute.flash_variable.date[0] = 2023 - 1900;
    hart_device_attribute.flash_variable.date[1] = 05;
    hart_device_attribute.flash_variable.date[2] = 10;

    osel_memcpy(hart_device_attribute.flash_variable.message, message, sizeof(message));
    osel_memcpy(hart_device_attribute.flash_variable.final_assembly_number, final_assembly_number, HART_PACKED3_LEN);
    hart_device_attribute.flash_variable.extended_device_type = EXTENDED_DEVICE_TYPE_LAPOOL;
    hart_device_attribute.flash_variable.manufacturer_identification_code = MANUFACTURER_IDENTIFIER_ACROMAG;
    hart_device_attribute.flash_variable.private_label_distributor_code = MANUFACTURER_IDENTIFIER_ACROMAG;
    hart_device_attribute.flash_variable.device_profile = DEVICE_CONFIGURATION_FILE_CODE_68;
    hart_device_attribute.flash_variable.device_revision = 1;
    hart_device_attribute.flash_variable.device_software_revision = 1;
    hart_device_attribute.flash_variable.device_hardware_revision.bits.hardware_revision = 1;
    hart_device_attribute.flash_variable.device_hardware_revision.bits.physical_signaling_code = PHYSICAL_SIGNALS_2;
    osel_memcpy(hart_device_attribute.flash_variable.unit_device.tag, "tm001", HART_PACKED6_LEN);
    osel_memcpy(hart_device_attribute.flash_variable.unit_device.descriptor, "temperature", HART_PACKED12_LEN);
    hart_device_attribute.flash_variable.unit_device.date[0] = 10;
    hart_device_attribute.flash_variable.unit_device.date[1] = 05;
    hart_device_attribute.flash_variable.unit_device.date[2] = 2023 - 1900;
    hart_device_attribute.flash_variable.country_code = 86;

    // 趋势配置
    trend_configuration_t *trend_configuration = &hart_device_attribute.flash_variable.trend_configurations[0];
    trend_configuration->device_variable_code = 0;
    trend_configuration->device_variable_classification = DEVICE_VARIABLE_CLASSIFICATION_CODE_64;
    trend_configuration->device_variable_unit_code = UNITS_CODE_TEMPERATURE_CELSIUS;
    trend_configuration->trend_code = TREND_CONTROL_CODE_1;
    trend_configuration->trend_sample_interval = 3600 * 1000;
    trend_configuration->trend_value_date[0] = 10;
    trend_configuration->trend_value_date[1] = 05;
    trend_configuration->trend_value_date[2] = 2023 - 1900;
    trend_configuration->trend_value_time = (12 * 3600 + 1 * 60 + 0) * 1000;

    // 设备状态
    osel_memset((uint8_t *)&hart_device_attribute.device_status, 0, sizeof(hart_device_status_t));
    hart_device_attribute.device_status.operational_state = DEVICE_OPERATIONAL_STATE_0;
    hart_device_attribute.device_status.additional_device_status.extended_device_status = EXTENDED_DEVICE_STATUS_1;
    hart_device_attribute.device_status.additional_device_status.device_operating_mode = OPERATING_MODE_CODES_0;
    hart_device_attribute.device_status.additional_device_status.standardized_status0 = STANDARDIZED_STATUS0_2;
    hart_device_attribute.device_status.additional_device_status.standardized_status1 = STANDARDIZED_STATUS1_3;
    hart_device_attribute.device_status.additional_device_status.analog_channel_saturated = ANALOG_CHANNEL_SATURATED_1;
    hart_device_attribute.device_status.additional_device_status.standardized_status2 = STANDARDIZED_STATUS2_0;
    hart_device_attribute.device_status.additional_device_status.standardized_status3 = STANDARDIZED_STATUS3_5;
    hart_device_attribute.device_status.additional_device_status.analog_channel_fixed = ANALOG_CHANNEL_FIXED_1;
    hart_additional_device_status_crc_init();

    // 设备变量
    hart_device_variable_t *hart_device_variable_p = hart_device_attribute.flash_variable.device_variable;
    hart_device_variable_p->code = DEVICE_VARIABLE_CODE_242; // 电池电压
    hart_device_variable_p->classification = DEVICE_VARIABLE_CLASSIFICATION_CODE_0;
    hart_device_variable_p->family = DEVICE_VARIABLE_FAMILY_CODE_250;
    hart_device_variable_p->units_code = UNITS_CODE_ELECTROMOTANCE_VOLTS;
    hart_device_variable_p->acquisition_period = 1000;
    hart_device_variable_p->value = &default_value;
    hart_device_variable_p++;
    hart_device_variable_p->code = DEVICE_VARIABLE_CODE_243; // 电池寿命
    hart_device_variable_p->classification = DEVICE_VARIABLE_CLASSIFICATION_CODE_0;
    hart_device_variable_p->family = DEVICE_VARIABLE_FAMILY_CODE_250;
    hart_device_variable_p->units_code = UNITS_CODE_TIME_D;
    hart_device_variable_p->acquisition_period = 1000;
    hart_device_variable_p->value = &battery_day;
    hart_device_variable_p++;
    hart_device_variable_p->code = DEVICE_VARIABLE_CODE_244; // 百分百
    hart_device_variable_p->classification = DEVICE_VARIABLE_CLASSIFICATION_CODE_0;
    hart_device_variable_p->family = DEVICE_VARIABLE_FAMILY_CODE_250;
    hart_device_variable_p->units_code = UNITS_CODE_OTHER_PERCENT;
    hart_device_variable_p->value = &primary_variable_range_percentage;
    hart_device_variable_p++;
    hart_device_variable_p->code = DEVICE_VARIABLE_CODE_245; // 回路电流
    hart_device_variable_p->classification = DEVICE_VARIABLE_CLASSIFICATION_CODE_0;
    hart_device_variable_p->family = DEVICE_VARIABLE_FAMILY_CODE_250;
    hart_device_variable_p->units_code = UNITS_CODE_CURRENT_MA;
    hart_device_variable_p->acquisition_period = 1000;
    hart_device_variable_p->value = &primary_variable_loop_current;
    hart_device_variable_p++;
    hart_device_variable_p->code = DEVICE_VARIABLE_CODE_246; // 主变量
    hart_device_variable_p->dynamic_variable_code = DEVICE_VARIABLE_CODE_250;
    hart_device_variable_p->classification = DEVICE_VARIABLE_CLASSIFICATION_CODE_64;
    hart_device_variable_p->family = DEVICE_VARIABLE_FAMILY_CODE_4;
    hart_device_variable_p->acquisition_period = 1000;
    hart_device_variable_p->units_code = UNITS_CODE_TEMPERATURE_CELSIUS;
    hart_device_variable_p->value = &default_value;
    hart_device_variable_p->properties = DEVICE_VARIABLE_PROPERTY_FLAGS_8;
    hart_device_variable_p->attribute.alarm_selection_code = ALARM_CODE_239;
    hart_device_variable_p->attribute.transfer_function_code = TRANSMISSION_FUNCTION_CODE_LINEAR;
    hart_device_variable_p->attribute.upper_and_lower_range_values_units_code = UNITS_CODE_TEMPERATURE_CELSIUS;
    hart_device_variable_p->attribute.damping_value = 1.0;
    hart_device_variable_p->attribute.upper_range_value = 20.0;
    hart_device_variable_p->attribute.lower_range_value = 4.0;
    hart_device_variable_p->attribute.analog_channel_flags = ANALOG_CHANNEL_CODE_0;
    osel_memcpy((uint8_t *)&hart_device_variable_p->transducer.serial_number, transducer_serial_number, sizeof(uint24_t));
    hart_device_variable_p->transducer.limits_and_minimum_span_units_code = UNITS_CODE_CURRENT_MA;
    hart_device_variable_p->transducer.upper_limit.f = 20.0;
    hart_device_variable_p->transducer.lower_limit.f = 4.0;
    hart_device_variable_p->transducer.minimum_span.f = 1;
    analog_channel = &hart_device_attribute.flash_variable.analog_channel[ANALOG_CHANNEL_NUMBER_CODES_0];
    analog_channel->code = ANALOG_CHANNEL_NUMBER_CODES_0;
    analog_channel->units_code = UNITS_CODE_TEMPERATURE_CELSIUS;
    analog_channel->level.f = 0;
    analog_channel->percent_range.f = 0;
    analog_channel->attribute.alarm_selection_code = ALARM_CODE_239;
    analog_channel->attribute.transfer_function_code = TRANSMISSION_FUNCTION_CODE_LINEAR;
    analog_channel->attribute.upper_and_lower_range_values_units_code = UNITS_CODE_TEMPERATURE_CELSIUS;
    analog_channel->attribute.damping_value = 1.0;
    analog_channel->attribute.upper_range_value = 20.0;
    analog_channel->attribute.lower_range_value = 4.0;
    analog_channel->attribute.analog_channel_flags = ANALOG_CHANNEL_CODE_0;

    hart_device_variable_p++;
    hart_device_variable_p->code = DEVICE_VARIABLE_CODE_247; // 次要变量
    hart_device_variable_p->dynamic_variable_code = DEVICE_VARIABLE_CODE_250;
    hart_device_variable_p->classification = DEVICE_VARIABLE_CLASSIFICATION_CODE_65;
    hart_device_variable_p->family = DEVICE_VARIABLE_FAMILY_CODE_5;
    hart_device_variable_p->acquisition_period = 1000;
    hart_device_variable_p->units_code = UNITS_CODE_PRESSURE_KPA;
    hart_device_variable_p->value = &default_value;
    analog_channel = &hart_device_attribute.flash_variable.analog_channel[ANALOG_CHANNEL_NUMBER_CODES_1];
    analog_channel->code = ANALOG_CHANNEL_NUMBER_CODES_1;
    analog_channel->units_code = UNITS_CODE_PRESSURE_KPA;
    analog_channel->level.f = 0;
    analog_channel->percent_range.f = 0;

    hart_device_variable_p++;
    hart_device_variable_p->code = DEVICE_VARIABLE_CODE_248; // 三级变量
    hart_device_variable_p->dynamic_variable_code = DEVICE_VARIABLE_CODE_250;
    hart_device_variable_p->classification = DEVICE_VARIABLE_CLASSIFICATION_CODE_66;
    hart_device_variable_p->family = DEVICE_VARIABLE_FAMILY_CODE_6;
    hart_device_variable_p->acquisition_period = 1000;
    hart_device_variable_p->units_code = UNITS_CODE_VOLUMETRIC_FLOW_RATE_M3S;
    hart_device_variable_p->value = &default_value;
    analog_channel = &hart_device_attribute.flash_variable.analog_channel[ANALOG_CHANNEL_NUMBER_CODES_2];
    analog_channel->code = ANALOG_CHANNEL_NUMBER_CODES_2;
    analog_channel->units_code = UNITS_CODE_VOLUMETRIC_FLOW_RATE_M3S;
    analog_channel->level.f = 0;
    analog_channel->percent_range.f = 0;

    hart_device_variable_p++;
    hart_device_variable_p->code = DEVICE_VARIABLE_CODE_249; // 四级变量
    hart_device_variable_p->dynamic_variable_code = DEVICE_VARIABLE_CODE_250;
    hart_device_variable_p->classification = DEVICE_VARIABLE_CLASSIFICATION_CODE_67;
    hart_device_variable_p->family = DEVICE_VARIABLE_FAMILY_CODE_12;
    hart_device_variable_p->acquisition_period = 1000;
    hart_device_variable_p->units_code = UNITS_CODE_SPEED_MS;
    hart_device_variable_p->value = &default_value;
    analog_channel = &hart_device_attribute.flash_variable.analog_channel[ANALOG_CHANNEL_NUMBER_CODES_3];
    analog_channel->code = ANALOG_CHANNEL_NUMBER_CODES_3;
    analog_channel->units_code = UNITS_CODE_SPEED_MS;
    analog_channel->level.f = 0;
    analog_channel->percent_range.f = 0;

    hart_device_variable_p++;

    hart_device_attribute.last_device_variable = &last_code;
    hart_device_attribute.configuration_update_count = &configuration_update_count;

    hart_device_attribute.flash_variable.poll_address = poll_address;
    osel_memcpy(hart_device_attribute.flash_variable.long_address, long_address, HART_LONG_ADDRESS_LEN);
    hart_device_attribute.actual_pv_current_level = &default_value;
}

void mock_data(void)
{
    if (TRUE == has_mock)
    {
        return;
    }
    has_mock = TRUE;
    mock_device_attribute();
    mock_user_attribute();
    mock_user_dynamics();
}
